<template>
  <div class="head-page">
    <el-header>
      <el-col :span="12" class="left">
        <img src="@/assets/logo.jpg" alt="Logo" class="logo" />
        <span class="system-name">智慧眼科医疗平台</span>
      </el-col>
      <el-col :span="12" class="right">
        <img src="@/assets/OIP-C.jpg" class="logo" />
        <span>{{ username }}</span>
        <el-button type="primary" @click="handleLogout">退出登录</el-button>
      </el-col>
    </el-header>

    <el-container>
      <el-aside width="200px">
        <el-menu default-active="1" class="el-menu-vertical-demo">
          <el-menu-item index="1" @click="router.push('/admin/user-management')"
            >用户管理</el-menu-item
          >
          <el-menu-item
            index="2"
            @click="router.push('/admin/feedback-management')"
            >反馈管理</el-menu-item
          >
        </el-menu>
      </el-aside>

      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { ref, getCurrentInstance, onMounted } from "vue";
const instance = getCurrentInstance();
const proxy = instance?.proxy as any;
import { useRouter, Router } from "vue-router";
const router: Router = useRouter();

import { useUserStore } from "@/stores/userStore";
const userStore = useUserStore();
import { RequestParams } from "@/types/request";

const username = ref<string>("管理员");
const token = ref<string>("");
const userData = userStore.getUserData();

const handleLogout = async () => {
  try {
    let result: any = await proxy.request({
      method: "post",
      url: proxy.api.usersLogout,
      params: {} as RequestParams,
      headers: {
        authentication: token.value,
      },
    });
    if (!result) {
      return;
    }
    if (result.code === 1) {
      proxy.message.success("已退出登录");
      userStore.clearUserData();
      router.push("/login");
    } else if (result.code === 401) {
      proxy.message.warning("身份过期，退出登录失败");
    }
  } catch (error) {
    proxy.message.warning("退出登录失败，请检查网络连接");
    userStore.clearUserData();
    router.push("/login");
  }
};

onMounted(() => {
  if (userData) {
    username.value = userData.username;
    token.value = userData.token;
    router.push("/admin/user-management");
  } else {
    proxy.message.error("未找到用户信息，请重新登录");
    router.push("/login");
  }
});
</script>

<style scoped>
.head-page {
  height: 100%;
  background-color: #f0f4f8; /* 淡蓝灰色背景 */
}

.el-header {
  border-radius: 8px;
  background-color: #409eff;
  color: white;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 0 auto;
  width: 100%;
}

.el-header .left {
  display: flex;
  align-items: center;
}

.el-header .logo {
  height: 40px;
  margin-right: 10px;
  border-radius: 100%;
}

.el-header .system-name {
  font-size: 24px;
  font-weight: bold;
}

.el-header .right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

.el-header .right .el-avatar {
  margin-right: 10px;
}

.el-container {
  margin-top: 20px;
  display: flex;
  height: calc(100vh - 100px);
}

.el-aside {
  background-color: #fff;
  border-right: 1px solid #ebeef5;
  border-radius: 10px;
}

.el-main {
  background-color: #f8f8f8; /* 奶白色背景 */
  border-radius: 10px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 20px;
  width: 100%;
  margin-left: 20px;
  overflow: auto;
}
</style>
